package com.example.leo.mycharmingstudent.a1012;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.widget.TextView;

import com.example.leo.mycharmingstudent.R;

public class PropertyAnimatorActivity extends AppCompatActivity {

    //属性动画之透明度执行的TextView
    private TextView tvAlpha;
    //属性动画之旋转执行的TextView
    private TextView tvRotate;
    //属性动画之动画集合执行的TextView
    private TextView tvSet;
    //属性动画之动画集合(第二种实现方式)执行的TextView
    private TextView tvSet2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property_animator);

        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        tvAlpha = findViewById(R.id.tv_alpha_activity_property_animator);
        tvRotate = findViewById(R.id.tv_rotate_activity_property_animator);
        tvSet = findViewById(R.id.tv_set_activity_property_animator);
        tvSet2 = findViewById(R.id.tv_set2_activity_property_animator);
    }

    private void initData() {

    }

    private void initEvent() {
        tvAlpha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //执行属性动画之透明度
                Animator alphaAnimator = AnimatorInflater.loadAnimator(
                        PropertyAnimatorActivity.this,
                        R.animator.alpha);
                alphaAnimator.setTarget(tvAlpha);
                alphaAnimator.start();
            }
        });
        tvRotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //执行属性动画之旋转
                ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(
                        tvRotate, "rotation", 0, 720);
                rotationAnimator.setDuration(1000);
                rotationAnimator.start();
            }
        });

        tvSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //执行属性动画之动画集合
                ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(tvSet, "rotation", 0, 720);
                rotationAnimator.setDuration(1000);

                ObjectAnimator translateAnimator = ObjectAnimator.ofFloat(tvSet, "x", 0, 300);
                translateAnimator.setDuration(1000);

                ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(tvSet, "scaleX", 1.0f, 3.0f);
                scaleXAnimator.setDuration(1000);

                AnimatorSet animatorSet = new AnimatorSet();
                //playTogether 动画一起执行
//                animatorSet.playTogether(rotationAnimator,translateAnimator,scaleXAnimator);
                //playSequentially 动画按照设置的顺序执行
                animatorSet.playSequentially(rotationAnimator, translateAnimator, scaleXAnimator);

                animatorSet.start();
            }
        });

        tvSet2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //执行属性动画集合
                /*tvSet2.animate()
                        .rotation(720)
                        .setDuration(1000)
                        .start();
                tvSet2.animate()
                        .translationX(300)
                        .setDuration(2000)
                        .setStartDelay(1000)
                        .start();*/
                ViewPropertyAnimator rotationAnimator = tvSet2.animate();
                rotationAnimator.rotation(720);
                rotationAnimator.setDuration(1000);
                rotationAnimator.start();

                ViewPropertyAnimator translateAnimator = tvSet2.animate();
                translateAnimator.translationX(300);
                translateAnimator.setDuration(1000);
                translateAnimator.setStartDelay(1000);
                translateAnimator.start();

            }
        });
    }
}
